﻿using System;

namespace Event.Controls.Common
{
    public partial class Paging : System.Web.UI.UserControl
    {

        public int PageSize;
        public int Size;
        public int PageIndex = 1;
        public int SumPage;


        private string _buildLink = "<a href=\"/{1}trang-{2}.htm\" class=\"{5}\">{0}{3}{4}</a>";
        private string strTrang = string.Empty;


        public string LinkSite;

        public bool IsSearch = false;
        public int Round = 1;
        public string Keyword = string.Empty;

        private string currentPage = "1";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool Padding(int pageIndex)
        {
            if (IsSearch && Round == -1) _buildLink = "<a href=\"/{1}.htm?k=" + Keyword + "&pi={2}\" class=\"{5}\">{0}{3}{4}</a>";
            else if (IsSearch) _buildLink = "<a href=\"/{1}.htm?k=" + Keyword + "&round=" + Round + "&pi={2}\" class=\"{5}\">{0}{3}{4}</a>";

            currentPage = pageIndex.ToString();
            var Text = string.Empty;
            int iCurrentPage = pageIndex;
            if (iCurrentPage == 0) iCurrentPage = 1;
            ltrList.Text = string.Empty;
            var currentBegin = string.Empty;
            var currentEnd = string.Empty;

            if (SumPage <= 4)
            {
                if (SumPage != 1)
                {
                    for (int i = 1; i <= SumPage; i++)
                    {
                        currentBegin = string.Empty;
                        currentEnd = string.Empty;
                        Text += String.Format(_buildLink, currentBegin, LinkSite, strTrang + i, i, currentEnd, currentPage == i.ToString() ? "active" : string.Empty);
                    }
                }
                else return false;
            }
            else
            {
                if (iCurrentPage > 1)
                {
                    Text += String.Format(_buildLink, "Trang đầu", LinkSite, strTrang + "1", string.Empty, string.Empty, "first");
                }
                else
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        currentBegin = string.Empty;
                        currentEnd = string.Empty;
                        Text += String.Format(_buildLink, currentBegin, LinkSite, strTrang + i.ToString(), i, currentEnd, currentPage == i.ToString() ? "active" : string.Empty);
                    }
                    Text += String.Format(_buildLink, ">", LinkSite, strTrang + "4", string.Empty, string.Empty, "next");
                }
                if (iCurrentPage > 1 && iCurrentPage < SumPage)
                {
                    if (iCurrentPage > 2)
                    {
                        Text += String.Format(_buildLink, "<", LinkSite, strTrang + (iCurrentPage - 2).ToString(), string.Empty, string.Empty, "pre");
                    }
                    for (int i = (iCurrentPage - 1); i <= (iCurrentPage + 1); i++)
                    {
                        currentBegin = string.Empty;
                        currentEnd = string.Empty;
                        Text += String.Format(_buildLink, currentBegin, LinkSite, strTrang + i.ToString(), i, currentEnd, currentPage == i.ToString() ? "active" : string.Empty);
                    }
                    if (iCurrentPage <= SumPage - 2)
                    {
                        Text += String.Format(_buildLink, ">", LinkSite, strTrang + (iCurrentPage + 2).ToString(), string.Empty, string.Empty, "next");
                    }
                }
                int intCurrentPage;
                int.TryParse(currentPage, out intCurrentPage);
                if (intCurrentPage == 0) intCurrentPage = 1;
                if (intCurrentPage < SumPage)
                {
                    Text += String.Format(_buildLink, "Trang cuối", LinkSite, strTrang + SumPage.ToString(), string.Empty, string.Empty, "last");
                }
                else
                {
                    Text += String.Format(_buildLink, "<", LinkSite, strTrang + (SumPage - 4).ToString(), string.Empty, string.Empty, "pre");
                    int j = 3;
                    for (int i = SumPage; i >= SumPage - 3; i--)
                    {
                        currentBegin = string.Empty;
                        currentEnd = string.Empty;
                        Text += String.Format(_buildLink, currentBegin, LinkSite, strTrang + (SumPage - j).ToString(), SumPage - j, currentEnd, currentPage == (SumPage - j).ToString() ? "active" : string.Empty);
                        j--;
                    }

                }
            }
            ltrList.Text = Text;
            return true;
        }
    }
}